package com.xxworkshop.dtcqcalculator;

import android.content.Context;
import com.xxworkshop.database.SqliteDatabase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by brochexu on 10/19/14.
 */
public class DataManager extends SqliteDatabase {
    private Context context;
    private static DataManager instance;

    public static DataManager getInstance() {
        return instance;
    }

    public DataManager(Context context, String name, int version) {
        super(context, name, version);
        this.context = context;
        instance = this;
        instance.setCreateResource(R.raw.cache);
    }

    public void setHeroItems(int profileid, List<HeroItem> his) {
        execute("delete from profiles where profileid='" + profileid + "'");
        for (HeroItem hi : his) {
            execute("insert into profiles(profileid,value) values('" + profileid + "', '" + hi.toString() + "')");
        }
    }

    public List<HeroItem> getHeroItems(int profileid) {
        List<HeroItem> result = new ArrayList<HeroItem>();
        List<Map<String, Object>> tres = query("select * from profiles where profileid='" + profileid + "'");
        for (Map<String, Object> t : tres) {
            try {
                result.add(new HeroItem(t.get("value").toString()));
            } catch (Exception e) {
            }
        }

        return result;
    }

    public List<Map<String, Object>> getProfiles() {
        return query("select distinct profileid from profiles order by profileid");
    }

    public int getMaxProfileId() {
        Object obj = scalar("select max(profileid) from profiles");
        if (obj != null) {
            return Integer.parseInt(obj.toString());
        } else {
            return 0;
        }
    }

    public List<Map<String, Object>> getHeros() {
        return query("select id,name from hero order by sort");
    }

    public List<Map<String, Object>> getRanks() {
        return query("select id,name from rank order by id desc");
    }

    public String getHeroName(long id) {
        if (id >= 0) {
            return scalar("select name from hero where id=" + id).toString();
        } else {
            return "";
        }
    }

    public String getRankName(long id) {
        if (id > 0) {
            return scalar("select name from rank where id=" + id).toString();
        } else {
            return "";
        }
    }

    public List<Map<String, Object>> getEquipments(long heroid, long rankid) {
        return query("select b.id,b.name from heroequipment a left join equipment b on a.equipmentid=b.id where a.heroid=" + heroid + " and a.rankid=" + rankid);
    }

    public List<Map<String, Object>> calculate(long targetrankid, boolean flag) {
        List<HeroItem> heros = Global.getInstance().heros;
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (HeroItem hi : heros) {
            result.addAll(calculate1(hi, targetrankid));
        }
        List<String> sqls = new ArrayList<String>();
        execute("delete from tmp_equipment");
        for (Map<String, Object> item : result) {
            sqls.add("insert into tmp_equipment(id, name,requirelevel,quality,pieces,isleaf) values('" + item.get("id") + "','" + item.get("name") + "','" + item.get("requirelevel") + "','" + item.get("quality") + "','" + item.get("pieces") + "','" + item.get("isleaf") + "')");
        }
        executeBatch(sqls);
        if (flag) {
            return query("select name,count(1) count, sum(pieces) pieces, requirelevel from tmp_equipment where quality>=3 group by name order by requirelevel");
        } else {
            return query("select name,count(1) count, sum(pieces) pieces, requirelevel from tmp_equipment group by name order by requirelevel");
        }
    }

    private List<Map<String, Object>> calculate1(HeroItem heroItem, long targetrankid) {
        List<Map<String, Object>> items = query("select b.* from heroequipment a left join equipment b on a.equipmentid=b.id where a.heroid=" + heroItem.heroid + " and a.rankid>=" + heroItem.rankid + " and a.rankid<=" + targetrankid);
        for (long equipmentid : heroItem.equipments) {
            for (Map<String, Object> item : items) {
                if (Long.parseLong(item.get("id").toString()) == equipmentid) {
                    items.remove(item);
                    break;
                }
            }
        }

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> item : items) {
            result.addAll(calculate2(item));
        }
        return result;
    }

    private List<Map<String, Object>> calculate2(Map<String, Object> item) {
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        if (item.get("isleaf").toString().equals("1")) {
            result.add(item);
        } else {
            List<Map<String, Object>> tresult = query("select b.* from equipmentrelation a left join equipment b on a.childequipmentid=b.id where a.equipmentid=" + item.get("id"));
            for (Map<String, Object> ttresult : tresult) {
                result.addAll(calculate2(ttresult));
            }
        }
        return result;
    }
}
